<template>
	<div id="mailList">
		<div class="i_maxW">
			<div class="u_crumbs">
				<router-link to="/personal/index">个人中心</router-link>
				<strong><i>></i>通讯录</strong>
			</div>
			<!--  -->
			<div class="u_content privateLetteBox">
				<div class="content_l box">
					<div class="communicationBox">
						<div class="communication">
							<router-link to="/personal/contact/privateLette">私信</router-link>
							<router-link to="/personal/contact/mailList">通讯录</router-link>
						</div>
						<div class="communication_c">
							<el-scrollbar>
								<!-- 多级 & 教育局 -->
								<template v-if="$store.state.isSchool && $store.state.isCheckLevel">
									<el-collapse v-model="activeNamesP">
								
										<el-collapse-item title="教研人员" name="44" class="userGroup_b1">
											<div class="userGroup">
												<label v-for="item in academicList.user_list" :key="item.id">
													<input type="radio" name="group">
													<div class="userGroup_txt" @click="changeUserid(item)">
														<div class="box">
															<img :src="BASE_URL+item.avatar_path" :onerror="errorImg">
														</div>
														<div class="box">
															<p>{{item.name}}</p>
															<p>
																{{item.class_name}}
																<span v-if="item.is_base === 1"><i> / </i>班主任</span>
															</p>
														</div>
													</div>
												</label>
											</div>
										</el-collapse-item>
									</el-collapse>
								</template>

								<el-collapse v-model="activeNames">
									<template>
										<template v-if="identity.includes(+teacheID) || $store.state.isSchool">
											<el-collapse-item title="老师" name="1" class="userGroup_b1">
												<div class="userGroup">
													<label v-for="item in teacherList.user_list" :key="item.id">
														<input type="radio" name="group">
														<div class="userGroup_txt" @click="changeUserid(item)">
															<div class="box">
																<img :src="BASE_URL+item.avatar_path" :onerror="errorImg">
															</div>
															<div class="box">
																<p>{{item.name}}</p>
																<p v-if='item.platform_name'>来自： <span>{{item.platform_name}}</span></p>
																<p v-if='!$store.state.isSchool'>
																	{{item.class_name}}
																	<span v-if="item.is_base === 1"><i> / </i>班主任</span>
																</p>
															</div>
														</div>
													</label>
												</div>
											</el-collapse-item>
										</template>
										<!--  -->
										<template v-else>
											<el-collapse v-model="activeNamesT">
												<el-collapse-item title="老师" class="userGroup_b1" name="11">
													<template v-for="item in teacherList">
														<el-collapse v-model="activeNamesT2" :key="item.id + 1">
															<el-collapse-item :title="item.rename" class="userGroup_child" :name="item.id">
																<label v-for="item2 in item.user_list" :key="item2.id+2">
																	<input type="radio" name="group">
																	<div class="userGroup_txt" @click="changeUserid(item2)">
																		<div class="box">
																			<img v-if="item2.avatar_path !== ''" :src="BASE_URL+item2.avatar_path" :onerror="errorImg">
																			<img v-else src="../../../public/images/Group9.png">
																		</div>
																		<div class="box">
																			<p>{{item2.name}}</p>
																			<p>
																				{{item2.class_name}}
																				<span v-if="item2.is_base === 1"><i> / </i>班主任</span>
																			</p>
																		</div>
																	</div>
																</label>
															</el-collapse-item>
														</el-collapse>
													</template>
												</el-collapse-item>
											</el-collapse>
										</template>
									</template>

									<template v-if="!$store.state.isSchool">
										<template>
											<el-collapse v-model="activeNamesS">
												<el-collapse-item title="同学" class="userGroup_b2" name="22">
													<template v-for="item in studentList">
														<el-collapse v-model="activeNamesS2" :key="item.id">
															<el-collapse-item :title="item.rename" class="userGroup_child" :name="item.id+3">
																<label v-for="item2 in item.user_list" :key="item2.id">
																	<input type="radio" name="group">
																	<div class="userGroup_txt" @click="changeUserid(item2)">
																		<div class="box">
																			<img :src="BASE_URL+item2.avatar_path">
																		</div>
																		<div class="box">
																			<p>{{item2.name}}</p>
																			<p>{{item.rename}}</p>
																		</div>
																	</div>
																</label>
															</el-collapse-item>
														</el-collapse>
													</template>
												</el-collapse-item>
											</el-collapse>
										</template>
										<!--  -->
										<template>
											<el-collapse v-model="activeNamesP">
												<el-collapse-item title="家长" class="userGroup_b3" name="33">
													<template v-for="item in parentList">
														<el-collapse v-model="activeNamesP2" :key="item.id">
															<el-collapse-item :title="item.rename" class="userGroup_child" :name="item.id+3">
																<label v-for="item2 in item.user_list" :key="item2.id">
																	<input type="radio" name="group">
																	<div class="userGroup_txt" @click="changeUserid(item2)">
																		<div class="box">
																			<img :src="BASE_URL+item2.avatar_path" :onerror="errorImg">
																		</div>
																		<div class="box">
																			<p>{{item2.name}}</p>
																		</div>
																	</div>
																</label>
															</el-collapse-item>
														</el-collapse>
													</template>
												</el-collapse-item>
											</el-collapse>
										</template>
									</template>

								</el-collapse>
							</el-scrollbar>
						</div>
						<!--无数据状态  -->
						<div class="usetInfo_cb" v-if="0">
							<span>- 关注的人会添加到通讯录 -</span>
						</div>
					</div>
				</div>
				<!--  -->
				<div class="content_r box  ">
					<div class="usetInfo" v-if="userID">
						<div class="usetInfo_txt">
							<div class="usetInfo_h">
								<div class="usetInfo_hp">
									<span @click="spaceURL(userID.id)">
										<img :src="BASE_URL+userID.avatar_path" v-if="userID.avatar_path !== ''" :onerror="errorImg">
										<img v-else src="../../../public/images/Group9.png">
									</span>
									<span>
										{{userID.name}}
									</span>
								</div>
								<router-link class="sendMsg" :to="{path: '/personal/contact/privateLette'}" v-if="$g.user_id !== userID.id">
									<i class="ipm icon_sixin"></i>
									发信息
								</router-link>
							</div>
							<!--  -->
							<div class="usetInfo_word">
								<span v-for="item of classUserid" :key="item.id">{{item.rename}}</span>
								<!-- 	<span v-if="userID.is_base === 1">班主任</span> -->
							</div>
							<ul class="usetInfo_wp">
								<li class="usetInfo_lx">
									<span>TA的简介</span>
									<div>
										<span v-if="userID.autograph ">{{userID.autograph}}</span>
										<strong v-else>- 暂无备注 -</strong>
									</div>
								</li>
								<li class="usetInfo_lx">
									<span>联系方式</span>
									<div>
										<span v-if="userID.telphone">{{userID.telphone}}</span>
										<strong v-else>- 暂无备注 -</strong>
									</div>
								</li>
								<li class="usetInfo_lx">
									<span>平台账号</span>
									<div>
										<span v-if="userID.number">{{userID.number}}</span>
										<strong v-else>- 暂无备注 -</strong>
									</div>
								</li>
								<li class="usetInfo_lx">
									<span>资源信息</span>
									<div>
										<span v-if="userID.resource_count !== 0">{{userID.resource_count}}</span>
										<strong v-else>- 暂无资源 -</strong>
									</div>
								</li>

								<li class="usetInfo_lx" v-if="userID.children && userID.children.length !== 0 ">
									<span>孩子Ta爸/妈</span>
									<div>
										<template  v-for='item of userID.children'>
											<span :key="item.name" >{{item.name}}</span>
										</template>
									</div>
								</li>
							</ul>
						</div>
					</div>
					<!-- 无数据状态 -->
					<div class="user_bgx" v-else>
						<i class="ipm slogn02"></i>
						<img src="../../../public/images/tongxunlu_image01.png">
					</div>
				</div>
			</div>
		</div>
	</div>
</template>
<script>

export default {
	name: '',
	data() {
		return {
			activeNames: [],
			activeNamesT: [],
			activeNamesT2: [],

			activeNamesS: [],
			activeNamesS2: [],

			activeNamesP: [],
			activeNamesP2: [],
			parentList: [],
			studentList: [],
			teacherList: [],
			academicList: [],
			userID: '',

			teacheID: '',
			identity: [6, 8],

			errorImg: 'this.src="' + require('../../../public/images/Group9.png') + '"',
			user: false,
			classUserid: []

		}
	},
	created() {
		this.getUserMsg();
		this.getidentity()
	},
	mounted() {},
	methods: {
		/*
			获取用户所在的班级
		*/
		getClassUserid(id) {
			let success = (res => {
				this.classUserid = res.data
			});

			let params = {
				user_id: id
			}
			this.$ajax.get('api/org/class_userid', { params, func: { success: success} })
	
		},
		/**
		 * 跳转到个人中心
		 * @DateTime 2020-03-05T14:25:31+0800
		 * @param    {[type]}                 id [description]
		 * @return   {[type]}                    [description]
		 */
		spaceURL(id) {
			let host = sessionStorage.getItem('host')
			let redirectParams = sessionStorage.getItem('redirect_params')
			let link = `${this.BASE_URL}${redirectParams}&host=${host}&routerUrl=space/video&id=${id}`
			window.open(link)
		},
		//获取身份
		getidentity() {
			let success = (res => {
				let data = res.data;
				for (var i = 0; i < data.length; i++) {
					if (this.identity.includes(+data[i].base_id)) {
						this.teacheID = data[i].base_id;
						break
					}
				}
			})

			let params = {
				userid: this.$g.user_id,
				type: 0
			}
			this.$ajax.get('api/user/get_all_role_by_userid', { params, func: { success: success} })
		},
		//获取数据
		getUserMsg() {
			let success = (res => {
				let data = res.data;
				this.userList = data.data;
				this.parentList = data.parent_list
				this.studentList = data.student_list;
				this.teacherList = data.teacher_list;
				this.academicList = data.academic_list

			})


			let params = { userid: this.$g.user_id };

			if (this.$store.state.isSchool && this.$store.state.isCheckLevel) {

				this.$ajax.get('api/user/platform_user_mail_list', { params: params, func: { success: success } })

			} else {
				this.$ajax.get('api/user/user_mail_list', { params: params, func: { success: success } })
			}
		},
		//用户所在的班级
		changeUserid(item) {
			this.userID = item
			this.getClassUserid(item.id)

			this.$store.commit('SET_SEND_USERINFO', {
				send_id: this.userID.id,
				avatar_path: this.userID.avatar_path,
				username: this.userID.name
			});
		}

	}
}
</script>